package com.xl.web.controller.zoom;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jodd.util.StringUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xl.base.annotation.Auth;
import com.xl.base.page.Page;
import com.xl.base.page.PageBean;
import com.xl.base.web.BaseController;
import com.xl.core.util.HtmlUtil;
import com.xl.core.util.zoom.ZoomRestAPI;
import com.xl.core.util.zoom.model.ZoomUserDelete;
import com.xl.web.entity.zoom.Company;
import com.xl.web.entity.zoom.SysUser;
import com.xl.web.service.zoom.CompanyService;
import com.xl.web.service.zoom.UserService;

@Controller
@Scope("prototype")
@RequestMapping("/user")
public class UserInfoController extends BaseController {
	@Autowired(required = false)
	private UserService<SysUser> userService;

	@Autowired(required = false)
	private CompanyService<Company> companyService;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ResponseBody
	@RequestMapping(value = "/queryPages", method = RequestMethod.GET)
	@Auth(verifyLogin = false)
	public void queryUserList(SysUser user, Page page) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		PageBean pageBean = userService.queryPageUserInfo(user, page);
		result.put(this.SUCCESS, true);
		result.put(this.MSG, "查询成功!");
		// result.put("count", userList != null ? userList.size() : 0);
		result.put("data", pageBean);
		HtmlUtil.writerJson(response, result);
	}

	@ResponseBody
	@RequestMapping("/queryUserInfo")
	@Auth(verifyLogin = false)
	public void queryUserList(SysUser user) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		List<SysUser> userList = userService.queryUserInfo(user);
		result.put(this.SUCCESS,true);
		result.put(this.MSG, "查询成功!");
		result.put("count", userList != null ? userList.size() : 0);
		result.put("data", userList);
		HtmlUtil.writerJson(response, result);
	}

	/**
	 * 支持用户名、用户账户、手机号码模糊查询
	 * 
	 * @param user
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/queryByKeyWord")
	@Auth(verifyLogin = false)
	public void queryUserByKeyWord(String keyWord) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		if (StringUtil.isEmpty(keyWord)) {
			this.sendFailureMessage(response, "查询关健字不能为空！");
			return;
		}
		List<Map> userList = userService.queryUserByKeyWord(keyWord);
		result.put(this.SUCCESS, true);
		result.put(this.MSG, "查询成功!");
		result.put("count", userList != null ? userList.size() : 0);
		result.put("attachUserList", userList);
		HtmlUtil.writerJson(response, result);
	}

	// 添加用户
	@ResponseBody
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@Auth(verifyLogin = false)
	public void addUser(SysUser user) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		if (StringUtil.isEmpty(user.getCompanyId())) {
			this.sendFailureMessage(response, "公司信息不能为空!");
			return;
		}
		if (StringUtil.isEmpty(user.getDeptCode())) {
			this.sendFailureMessage(response, "部门信息不能为空!");
			return;
		}
		if (StringUtil.isEmpty(user.getUserAccount())) {
			this.sendFailureMessage(response, "用户账户不能空!");
			return;
		}
		if (StringUtil.isEmpty(user.getPassword())) {
			this.sendFailureMessage(response, "用户密码不能为空!");
			return;
		}
		if (StringUtil.isEmpty(user.getEmail())) {
			this.sendFailureMessage(response, "邮箱不能为空!");
			return;
		}
		if (StringUtil.isEmpty(user.getUserName())) {
			this.sendFailureMessage(response, "用户名不能为空!");
			return;
		}
		SysUser addUser = new SysUser();
		addUser.setUserAccount(user.getUserAccount());
		List<SysUser> userList = userService.queryUserInfo(addUser);
		if (userList != null && userList.size() > 0) {
			this.sendFailureMessage(response, "【" + user.getUserAccount()
					+ "】用户账号已添加不能重复添加！");
			return;
		}
		// 1.判断账号添加是否受限制，2.调用账号添加api，3保存入库
		boolean isSaveAccount = userService.checkAccountLimit(user
				.getCompanyId());

		// 查询公司信息
		Company company = new Company(user.getCompanyId());
		List<Company> companys = companyService.queryCompanyList(company);
		if (companys != null && companys.size() > 0) {
			company = companys.get(0);
		} else {
			this.sendFailureMessage(response, "未获取到对应公司信息，请联系管理员配置!");
			return;
		}
		if (isSaveAccount) {// 公司添加账户数未达上限
			// 调用api创建zoom账kgn
			if (company.getAutoCreateZoom() == 1) { // 自动创建
				user.setCapacity(company.getDefaultCapacity());
				ZoomRestAPI.company = company;
				user = ZoomRestAPI.createUser(user);
				userService.saveUser(user);
			} else {
				userService.saveUser(user);
			}
		} else {
			this.sendFailureMessage(response, "公司添加账户数已达上限，请联系管理员!");
			return;
		}
		result.put(this.SUCCESS, true);
		result.put(this.MSG, "添加成功!");
		result.put("count", 1);
		result.put("data", user);
		HtmlUtil.writerJson(response, result);
	}

	// 删除用户
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@Auth(verifyLogin = false)
	public void delUser(
			@RequestParam(value = "userId", required = false) String userId)
			throws Exception {
		String msg = "删除成功!";
		if (userId != null && !"".equals(userId)) {
			// 根据userId查询companyId
			SysUser user = new SysUser(userId);
			List<SysUser> userList = userService.queryUserInfo(user);
			if (userList != null && userList.size() > 0) {
				user = userList.get(0);
				ZoomRestAPI.company = user.getCompany();
				ZoomUserDelete zoomUserDel = ZoomRestAPI.deleteUser(
						user.getHostId(), user.getEmail());
				if (zoomUserDel.getError().getCode() != null) {
					this.sendFailureMessage(response, zoomUserDel.getError()
							.getMessage());
					return;
				}
				userService.delUser(userId);
			} else {
				this.sendFailureMessage(response, "要删除的用户不存在！");
				return;
			}
		}
		this.sendFailureMessage(response, msg);

	}

	// 更新用户
	@ResponseBody
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@Auth(verifyLogin = false)
	public void editUser(SysUser user) throws Exception {
		if (StringUtil.isEmpty(user.getUserId())) {
			this.sendFailureMessage(response, "更新的用户ID为空，更新失败!");
			return;
		}
		userService.editUser(user);
		this.sendSuccessMessage(response, "更新成功！");
	}

}
